package es.castorm.arch.mapping.config;

import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.metadata.ClassMap;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * The Class JpaPostProcessor.
 */
public class MappingPostProcessor implements BeanPostProcessor {

	/** The packages to scan. */
	private MapperFactory mapperFactory;

	/**
	 * Instantiates a new jpa post processor.
	 * 
	 * @param packagesToScan the packages to scan
	 */
	public MappingPostProcessor(MapperFactory mapperFactory) {
		super();
		this.mapperFactory = mapperFactory;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

		if (bean instanceof ClassMap) {
			mapperFactory.registerClassMap((ClassMap<?, ?>) bean);
		}

		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

		return bean;
	}
}
